package cn.zhaoyuening.hnistalumnirecord.web.controller;

import cn.zhaoyuening.hnistalumnirecord.domain.ConfigItem;
import cn.zhaoyuening.hnistalumnirecord.domain.NewsModule;
import cn.zhaoyuening.hnistalumnirecord.tmp.ModuleType;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by zhao on 17-1-17.
 *
 */
@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController{



    //返回module的json数据
    @RequestMapping(value = "moduleJson.do",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String moduleJson() {
        Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                String name = fieldAttributes.getName();
                return name.equals("newsSet");
            }

            @Override
            public boolean shouldSkipClass(Class<?> aClass) {
                return false;
            }
        }).create();

        List<NewsModule> list = getModuleService().findAll();
        String jsonData = gson.toJson(list);
        return jsonData;
    }

    @RequestMapping(value = "selectModule.do")
    public String selectModule(@RequestParam("id")int id,Model model) {
        NewsModule module = getModuleService().findById(id);
        ConfigItem configItem = getConfigItemService().findById(1);
        model.addAttribute("module", module);
        model.addAttribute("configItem", configItem);

        if (module.getType() == ModuleType.MODULE_TYPE_ALUMNUS) {
            //校友模式
            return "xiaoyou";
        }else{
            //普通文章模式
            return "newsList";
        }
    }

}
